package com.fit.services;

import java.io.IOException;

import org.apache.tapestry5.*;
import org.apache.tapestry5.hibernate.HibernateSymbols;
import org.apache.tapestry5.hibernate.HibernateTransactionAdvisor;
import org.apache.tapestry5.hibernate.HibernateTransactionDecorator;
import org.apache.tapestry5.ioc.Configuration;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.MethodAdviceReceiver;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Local;
import org.apache.tapestry5.ioc.annotations.Match;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestFilter;
import org.apache.tapestry5.services.RequestHandler;
import org.apache.tapestry5.services.Response;
import org.hibernate.Session;

import org.slf4j.Logger;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;



import com.fit.beans.UserBean;
import com.fit.enums.AccountRole;
import com.fit.hibernate.BorrowsServiceHibernate;
import com.fit.hibernate.CarsServiceHibernate;
import com.fit.hibernate.ServiceHibernate;
import com.fit.hibernate.UsersServiceHibernate;
import com.fit.interfaces.User;
import com.fit.service.BorrowsI;
import com.fit.service.CarsI;
import com.fit.service.ServicesI;
import com.fit.service.Users;


/**
 * This module is automatically included as part of the Tapestry IoC Registry, it's a good place to
 * configure and extend Tapestry, or to place your own service definitions.
 */
public class AppModule
{
	
	public static AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
	
    public static void bind(ServiceBinder binder)
    {
    	
    //	binder.bind(Users.class,UsersServiceHibernate.class);
    	binder.bind(BorrowsI.class, BorrowsServiceHibernate.class);
    	binder.bind(CarsI.class, CarsServiceHibernate.class);
    	binder.bind(ServicesI.class, ServiceHibernate.class);
        
        
        
    }
    
    
    public static void contributeApplicationDefaults(
            MappedConfiguration<String, String> configuration)
    {
        
        configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");

        configuration.add(SymbolConstants.PRODUCTION_MODE, "false");

       
        configuration.add(SymbolConstants.APPLICATION_VERSION, "1.0.0-SNAPSHOT");
        configuration.add(HibernateSymbols.EARLY_START_UP, "true");
    }
    
    public static void contributeHibernateEntityPackageManager(
            Configuration<String> configuration) {
    configuration.add("com.fit.beans");
    }

@Match("*CarsI")
public static <T> T decorateTransactionally(
            HibernateTransactionDecorator decorator, Class<T> serviceInterface,
            T delegate, String serviceId) {
    return decorator.build(serviceInterface, delegate, serviceId);
}
@Match("*BorrowsI")
public static <T> T decorateTransactionallyBorrows(
            HibernateTransactionDecorator decorator, Class<T> serviceInterface,
            T delegate, String serviceId) {
    return decorator.build(serviceInterface, delegate, serviceId);
}

@Match("*Users")
public static <T> T decorateTransactionallyUsers(
            HibernateTransactionDecorator decorator, Class<T> serviceInterface,
            T delegate, String serviceId) {
    return decorator.build(serviceInterface, delegate, serviceId);
}
@Match("*ServicesI")
public static <T> T decorateTransactionallyServices(
            HibernateTransactionDecorator decorator, Class<T> serviceInterface,
            T delegate, String serviceId) {
	 	 
    return decorator.build(serviceInterface, delegate, serviceId);
}

@Match("*Service")
public static void adviseTransactions(HibernateTransactionAdvisor advisor, MethodAdviceReceiver receiver)
{
		advisor.addTransactionCommitAdvice(receiver);
};

    



     		
    
//    public static Borrows buildBorrows(){
//    	return new BorrowsBean();
//    }
//    
    public static Users buildUsers(@Inject Session session){
    	Users users = new UsersServiceHibernate(session);
    	User uu = (User) context.getBean("admin");
//    		new UserBean();
//    	uu.setUserName("admin");
//    	uu.setPassword("admin");
//    	uu.setRole(AccountRole.ADMINISTRATOR);
    	users.save(uu);
    	session.save(uu);
    	return  users;
    }

}
